﻿using H2O.Api.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Impl.Matchers;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace H2O.Api.Controllers
{
    /// <summary>
    /// 调度控制器
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class ScheduleController : Controller
    {
        private readonly ILogger _logger;
        private readonly ISchedulerFactory _schedulerFactory;

        public ScheduleController(
            ILogger<ScheduleController> logger,
            ISchedulerFactory schedulerFactory)
        {
            _logger = logger;
            _schedulerFactory = schedulerFactory;
        }

        /// <summary>
        /// 清除所有调度数据(含作业 + 触发器 + 日历)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionName(name: "clear")]
        public async Task<IActionResult> ClearAsync()
        {
            IScheduler scheduler = await _schedulerFactory.GetScheduler();
            await scheduler.Clear();
            return Ok(new ActionResultModel
            {
                Code = 0,
                Message = "completed clear",
            });
        }

        /// <summary>
        /// 暂停所有
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionName(name: "pause_all")]
        public async Task<IActionResult> PauseAllAsync()
        {
            IScheduler scheduler = await _schedulerFactory.GetScheduler();
            await scheduler.PauseAll();
            return Ok(new ActionResultModel
            {
                Code = 0,
                Message = "pause all triggers"
            });
        }

        /// <summary>
        /// 获取所有作业组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionName(name: "get_job_group_names")]
        public async Task<IActionResult> GetJobGroupNamesAsync()
        {
            IScheduler scheduler = await _schedulerFactory.GetScheduler();
            IReadOnlyCollection<string> jobGroupNames = await scheduler.GetJobGroupNames();
            return Ok(new ActionResultModel
            {
                Code = 0,
                Message = "ok",
                Data = jobGroupNames
            });
        }

        /// <summary>
        /// 获取所有作业详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionName(name: "get_job_details")]
        public async Task<IActionResult> GetJobDetailsAsync()
        {
            IEnumerable<IJobDetail> jobDetails =
                from scheduler in await _schedulerFactory.GetAllSchedulers()
                from jobGroupName in scheduler.GetJobGroupNames().Result
                from jobKey in scheduler.GetJobKeys(matcher: GroupMatcher<JobKey>.AnyGroup()).Result
                select scheduler.GetJobDetail(jobKey: jobKey).Result;
            return Ok(value: new ActionResultModel
            {
                Code = 0,
                Message = "ok",
                Data = jobDetails
            });
        }
    }
}
